package com.jrx.anytxn.param.service.product.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.product.PrAutoPaymentTableRep;
import com.jrx.anytxn.param.dto.product.PrAutoPaymentTableRes;
import com.jrx.anytxn.param.entity.PrAutoPaymentTable;
import com.jrx.anytxn.param.mapper.ext.ExtPrAutoPaymentTableMapper;
import com.jrx.anytxn.param.service.product.IPrAutoPaymentTableService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@CacheConfig(cacheNames = "AUTOPAYMENT")
@Service
public class PrAutoPaymentTableServiceImpl implements IPrAutoPaymentTableService {

    private static final Logger logger = LoggerFactory.getLogger(PrAutoPaymentTableServiceImpl.class);

    @Autowired
    private ISegmentService segmentService;
    @Resource
    private ExtPrAutoPaymentTableMapper extPrAutoPaymentTableMapper;

    @Cacheable(key="'autopayment_'+#tenantId+'_'+#id")
    @Override
    public PrAutoPaymentTableRes findByTenIdAndProductId(String tenantId, String id) throws TxnBizException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(id)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnBizException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrAutoPaymentTable prAutoPaymentTable = extPrAutoPaymentTableMapper.selectByTenIdAndProductId(tenantId, id);
        return BeanMapping.copy(prAutoPaymentTable, PrAutoPaymentTableRes.class);
    }

    @Override
    public PrAutoPaymentTableRes addPrAutoPaymentTable(PrAutoPaymentTableRep request) throws TxnBizException {
        PrAutoPaymentTable table = extPrAutoPaymentTableMapper.selectByTenIdAndProductId(request.getTenantId(), request.getProductId());
        if (null != table) {
            logger.error(TxnRespCode.DATA_EXISTS.getMsg());
            throw new TxnBizException(TxnRespCode.DATA_EXISTS.getMsg());
        }

        PrAutoPaymentTable prAutoPaymentTable = BeanMapping.copy(request, PrAutoPaymentTable.class);

        try {
            prAutoPaymentTable.setId(segmentService.getId(Constant.PR_AUTOPAYMENT_TABLE));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnBizException(ErrorMsgConstant.ERROR_ID);
        }
        prAutoPaymentTable.setRecordVersionNumber(Constant.VERSION_NUMBER);
        extPrAutoPaymentTableMapper.insertSelective(prAutoPaymentTable);
        return BeanMapping.copy(prAutoPaymentTable, PrAutoPaymentTableRes.class);
    }

    @CacheEvict(key="'autopayment_'+#request.tenantId+'_'+#request.id")
    @Override
    public PrAutoPaymentTableRes modifyPrAutoPaymentTable(PrAutoPaymentTableRep request) throws TxnBizException {
        PrAutoPaymentTable table = extPrAutoPaymentTableMapper.selectByTenIdAndProductId(request.getTenantId(), request.getId().toString());
        if (table == null) {
            logger.error(TxnRespCode.DATA_EXISTS.getMsg());
            throw new TxnBizException(TxnRespCode.DATA_EXISTS.getMsg());
        }
        PrAutoPaymentTable prAutoPaymentTable = BeanMapping.copy(request, PrAutoPaymentTable.class);
        extPrAutoPaymentTableMapper.updateByTenIdAndProductId(prAutoPaymentTable);
        return BeanMapping.copy(prAutoPaymentTable, PrAutoPaymentTableRes.class);
    }

    @CacheEvict(key="'autopayment_'+#tenantId+'_'+#id")
    @Override
    public Boolean deletePrAutoPaymentTable(String tenantId, String id) throws TxnBizException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(id)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnBizException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        int i = extPrAutoPaymentTableMapper.deleteByTenIdAndProductId(tenantId, id);
        return i > 0;
    }

    @Override
    public TxnPage<PrAutoPaymentTableRes> findAllPageByTenantId(Integer pageNum, Integer pageSize, String tenantId) throws TxnException {
        Page<PrAutoPaymentTableRes> page = PageHelper.startPage(pageNum, pageSize, Constant.ORDER_BY_SORT);
        List<PrAutoPaymentTable> tableList = extPrAutoPaymentTableMapper.selectByTenantId(tenantId);
        if (tableList.isEmpty()) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnBizException(TxnRespCode.NO_DATA.getMsg());
        }
        List<PrAutoPaymentTableRes> res = BeanMapping.copyList(tableList, PrAutoPaymentTableRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }
}
